<template>
	<div>
		<div>
		   <div>
		        <el-button type="primary" @click="addBtn" size="mini">添加</el-button>
		    </div>
		</div>
		<!-- <br>
		<div class="searchForm">
		    <div>
		        标题： <el-input size="small" v-model="title" 
				placeholder="请输入标题"  style="width:300px;"></el-input>
		    </div>
			<div>
			    <el-button type="primary" size="mini" @click="search">检索</el-button>
			    <el-button size="mini" @click="clearSearch">重置/检索</el-button>
			</div>
		</div> -->
		<br>
		<el-table  :data="list"
			 border  
			 style="width: 100%"
			>
			<el-table-column   align="center"    prop="title"    label="标题"></el-table-column>
			<el-table-column   align="center"    prop="content"    label="描述"></el-table-column>
			<!-- <el-table-column  label="板块"   align="center">
			     <template slot-scope="scope">
			        <div v-if="scope.row.plate">
						{{plateList.filter(item => item.id==scope.row.plate)[0].name}}
					</div>
			     </template>
			 </el-table-column> -->
			 <el-table-column  label="状态"   align="center">
			      <template slot-scope="scope">
			         <div>
						<el-tag v-if="scope.row.status==0" size="small" effect="dark" type="danger">禁用</el-tag>
						<el-tag v-if="scope.row.status==1" size="small" effect="dark">启用</el-tag>
			 		</div>
			      </template>
			  </el-table-column>
			<el-table-column   align="center"    prop="add_time"    label="添加时间"></el-table-column>
			<el-table-column   align="center"    prop="update_time"    label="修改时间"></el-table-column>
			
		   <el-table-column  label="操作"   align="center">
		        <template slot-scope="scope">
					<span style="margin-right: 10px;" class="button_text" @click="editRow(scope.row)">编辑</span>
					<span class="button_text" @click="delRow(scope.row.id)">删除</span>
		        </template>
		    </el-table-column>
		</el-table>
		<div class="page">
		    <el-pagination
		            background
		            @size-change="pageChange"
		            @current-change="handleCurrentChange"
		            :current-page="page"
		            :page-size="pageSize"
		            layout="total, sizes, prev, pager, next, jumper"
		            :total="page_totle">
		    </el-pagination>
		</div>
		<el-dialog
		        :title="addTitle"
				:close-on-click-modal="false"
				:modal-append-to-body="false"
				:append-to-body="true"
		        :visible.sync="formShow"
		        width="1000px"
		>
		  <div>
			  <el-form ref="form" :model="form" label-width="100px">
				  <!-- <el-form-item label="板块">
				      <el-select v-model="form.plate" size="small" placeholder="请选择">
				          <el-option
				            v-for="item in plateList"
				            :key="item.id"
				            :label="item.name"
				            :value="item.id">
				          </el-option>
				        </el-select>
				  </el-form-item> -->
				  <el-form-item label="标题">
				      <el-input v-model="form.title" size="small"></el-input>
				  </el-form-item>
				  <el-form-item label="内容">
					    <el-button size="mini" @click="isShow=true" type="primary">选择图片</el-button>
				  </el-form-item>
				  <el-form-item v-if="form.img_url">
					  <div style="display: flex;">
						  <div style="position: relative;margin-right: 10px;" >
							  <img style="width: 100px;height: 100px;" :src="form.img_url" alt="">
							  <i @click="delImg" class="el-icon-circle-close" style="font-size: 20px;position: absolute;top: 0;right:0;cursor: pointer;"></i>
						  </div>
					  </div>
				  </el-form-item>  
				  <el-form-item label="描述">
				      <el-input v-model="form.content" size="small"></el-input>
				  </el-form-item>
				  <el-form-item label="状态">
				      <el-radio-group v-model="form.status">
				        <el-radio label="0">禁用</el-radio>
				        <el-radio label="1">启用</el-radio>
				      </el-radio-group>
				    </el-form-item>
				  <el-form-item label="跳转需要实名">
				      <el-radio-group v-model="form.plate">
				        <el-radio label="0">不需要</el-radio>
				        <el-radio label="1">需要</el-radio>
				      </el-radio-group>
				    </el-form-item>
				  <el-form-item label="跳转链接">
				      <el-input type="textarea" 
						placeholder="填公众号链接跳转公众号,填小程序链接跳转小程序"
						v-model="form.url" size="small"></el-input>
				  </el-form-item>
				  <el-form-item >
				      <el-button type="primary" @click="confirm" size="small">确定</el-button>
				  </el-form-item>
			  </el-form>
		  </div>
		</el-dialog>
		
		<el-dialog title="选择图片"
				:close-on-click-modal="false"
				:modal-append-to-body="false"
				:append-to-body="true"
		        :visible.sync="isShow"
		        width="1050px">
			 <div style="padding-bottom: 30px;">
				 <imgSys @getImgData="getImgData" />
			 </div>
		     
		</el-dialog>
	</div>
</template>

<script>
  import imgSys from '@/components/imgSys'
	export default {
		data(){
			return {
				title:"",
				page:1,
				pageSize:10,
				form:{
					id:null,
					title:'',
					content:"",
					plate:'0', //板块
					status:'1',
					type:'8',
					url:'',
					img_url:""
				},
				addTitle:'新增',
				fileList:[],
				isShow:false
			}
		},
		components:{
			imgSys
		},
		mounted() {
			this.lodeList()
		},
		methods:{
			getImgData(val){
				// this.fileList.push(val)
				this.form.img_url = val
				this.isShow = false
			},
			delImg(i){
				// this.fileList.splice(i,1)
				this.form.img_url = ''
			},
			editRow(row){
				this.addTitle = '编辑'
				for(let i in this.form){
					this.form[i] = row[i]
				}
				// if(this.form.content){
				// 	this.fileList = JSON.parse(this.form.content)
				// }
				this.formShow = true
			},
			delRow(id){
				this.$confirm('确定删除', '提示', {
				          confirmButtonText: '确定',
				          cancelButtonText: '取消',
				          type: 'warning'
				        }).then(() => {
				          this.$post('/homeSet/delHomeShow',{ids:[id]},res=>{
							  this.lodeList()
						  })
				        })
			},
			clearSearch(){
				this.title = ''
				this.search()
			},
			confirm(){
				// if(this.fileList.length==0) return this.$message.error('请上传图片')
				// this.form.content = JSON.stringify(this.fileList)
				if(!this.form.img_url) return this.$message.error('请上传图片')
				this.$post('/homeSet/addEditHomeShow',this.form,res=>{
					if(res.data.code==200){
						this.$message.success('操作成功')
						this.formShow = false
						this.lodeList()
					}
				})
			},
			addBtn(){
				this.addTitle = '新增'
				this.form={
					id:null,
					title:'',
					content:"",
					plate:'0', //板块
					status:'1',
					type:'8',
					url:'',
					img_url:''
				}
				this.fileList = []
				this.formShow = true
			},
			lodeList(){
				let params = {
					type:this.form.type,
					title:this.title,
					page:this.page,
					pageSize:this.pageSize
				}
				this.$post('/homeSet/getHomeShowList',params,res=>{
					let data = res.data.data
					this.page_totle = data.count
					this.list = data.result
				})
			},
		}
	}
</script>

<style>
</style>